package com.example.mytestdemo.viewpage2.adapter

import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityViewpager2ListadapterItemLayoutBinding

/**
 * @author tgw
 * @date 2022/7/5
 * @describe
 */

interface ImyAdapterListener{
    fun listener(position: Int)
}

class MyViewPager2Adapter(val listener:ImyAdapterListener?=null) : RecyclerView.Adapter<MyViewPager2Adapter.PagerViewHolder>() {

    companion object {
        private const val TAG = "MyViewPager2Adapter原始"
    }


    private lateinit var mBind: ActivityViewpager2ListadapterItemLayoutBinding
    private var mList: List<String> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerViewHolder {
        mBind = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.activity_viewpager2_listadapter_item_layout,
            parent,
            false
        )
        Log.d(TAG, "onCreateViewHolder: ")
        return PagerViewHolder(mBind)
    }

    override fun onBindViewHolder(holder: PagerViewHolder, position: Int) {
        holder.bindData(mList[position],position,listener)
        Log.d(TAG, "onBindViewHolder: $position")
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        Log.d(TAG, "onAttachedToRecyclerView: ")
    }

    override fun onViewAttachedToWindow(holder: PagerViewHolder) {
        super.onViewAttachedToWindow(holder)
        Log.d(TAG, "onViewAttachedToWindow: ${holder.adapterPosition}")
    }

    override fun onViewDetachedFromWindow(holder: PagerViewHolder) {
        super.onViewDetachedFromWindow(holder)
        Log.d(TAG, "onViewDetachedFromWindow: ${holder.adapterPosition}")
    }

    override fun onBindViewHolder(
        holder: PagerViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        super.onBindViewHolder(holder, position, payloads)
        Log.d(TAG, "onBindViewHolder: payloads $position")
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        Log.d(TAG, "onDetachedFromRecyclerView: ")
    }

    override fun onViewRecycled(holder: PagerViewHolder) {
        super.onViewRecycled(holder)
        Log.d(TAG, "销毁回收后才会重新走onBindViewHolder-- onViewRecycled: ${holder.adapterPosition}")
    }


    fun setList(list: List<String>) {
        mList = list
    }

    override fun getItemCount(): Int {
        return mList.size
    }


    //	ViewHolder需要继承RecycleView.ViewHolder
    class PagerViewHolder(val mBind: ActivityViewpager2ListadapterItemLayoutBinding) :
        RecyclerView.ViewHolder(mBind.root) {

        private var colors = arrayOf("#CCFF99", "#41F1E5", "#8D41F1", "#FF99CC")

        fun bindData(data: String,position: Int,listener:ImyAdapterListener?=null) {
            mBind.tv.text = "我是viewpager2的MyViewPager2Adapter布局-----：$data"
            mBind.tv.setBackgroundColor(Color.parseColor(colors[position % 4]))
            mBind.tv.setOnClickListener(){
                listener?.listener(position)
            }
        }
    }



}

